//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;

namespace CommunityServer.Galleries.Controls
{

	public class SendToFriend : GalleryThemedControl
	{

		#region Child Controls

		private TextBox From;
		private TextBox To;
		private TextBox Subject;
		private TextBox Body;

		private IButton Submit;

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			From = (TextBox)FindControl( "SendToFriendFrom" );
			To = (TextBox)FindControl( "SendToFriendTo" );
			Subject = (TextBox)FindControl( "SendToFriendSubject" );
			Body = (TextBox)FindControl( "SendToFriendBody" );
			Submit = FindButton( "SendToFriendSubmit" );

			InitializeChildControls();
		}

		private void InitializeChildControls()
		{
			Submit.Click += new EventHandler(Submit_Click);
		}

		#endregion

		#region Event Overrides

		protected override void OnLoad(EventArgs e)
		{
			this.EnsureChildControls();
			if(!Page.IsPostBack)
				Submit.Text = ResourceManager.GetString( "Send" );
		}

		protected override void OnPreRender(EventArgs e)
		{
			Submit.Attributes.Add("onclick", "return SubmitSendToFriend('" + From.ClientID + "', '" + To.ClientID + "');");
		}

		#endregion

		#region Event Handlers

		private void Submit_Click(object sender, EventArgs e)
		{
			if((From.Text != "") && (To.Text != ""))
			{
				CSContext cSContext = CSContext.Current;
				GalleryEmails.SendToFriend(cSContext.ApplicationKey, cSContext.CategoryID, cSContext.PostID, To.Text, From.Text, Subject.Text, Body.Text);

				From.Text = "";
				To.Text = "";
				Subject.Text = "";
				Body.Text = "";

				Page.RegisterStartupScript("AlertSuccess", "<script language='JavaScript'>alert('Message successfully sent!');</script>");
			}
			else
				Page.RegisterStartupScript("AlertFailure", "<script language='JavaScript'>ToggleSendToFriend();document.getElementById('" + From.ClientID + "').focus();alert('Missing required fields.');</script>");
		}
		#endregion

	}
}
